package com.bnz.service.impl;

import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.Classes;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.pojo.StudentExample;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    //1. 列表所有学生
    @Override
    public List<Student> findAll() {
        //1.1 查询所有学生
        List<Student> students = studentMapper.selectByExample(null);
        //1.2 将学生与班级名称绑定
//        for (Student student : students) {
//            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
//            student.setCname(classes.getCname());
//        }
        //1.3 返回
        return students;

    }

    //2. 分页查询
    @Override
    public PageResult findByPage(int page, int pageSize) {
        //2.1 开始分页
        PageHelper.startPage(page,pageSize);
        //2.2 开始分页查询
        //2.2.1 定义实例查询
        StudentExample example = new StudentExample();
        //2.2.2 开始查询
        List<Student> students = studentMapper.selectByExample(example);
        //2.2.3 将学生关联的班级重新绑定
//        for (Student student : students) {
//            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
//            student.setCname(classes.getCname());
//        }
        //2.3 转换为page
        Page<Student> studentPage = (Page<Student>) students;
        //2.4 返回
        return new PageResult<>(studentPage.getTotal(),studentPage.getResult());
    }
}
